import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class APP {

    static Scanner scanner = new Scanner(System.in);
    static List<Student> students = new ArrayList();
    public static void main(String[] args) {

        while (true){
            System.out.println("-----欢迎来到学生管理系统-----");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入:");
            int select = scanner.nextInt();
            switch (select){
                case 1: addStudent();
                    break;
                case 2: deleteStudent();
                    break;
                case 3: updateStudent();
                    break;
                case 4: showStudent();
                    break;
                case 5: System.exit(0);
                    break;
            }
        }
    }
    static int ID = 1 ;
    static void addStudent(){

        int id = ID++;
        System.out.println("请输入学生姓名:");
        String name = scanner.next();
        System.out.println("请输入学生年龄:");
        int age = scanner.nextInt();
        System.out.println("请输入学生家庭住址:");
        String address = scanner.next();
        Student student = new Student(id, name, age, address);
        students.add(student);
        System.out.println("添加成功!");
    }
    static void deleteStudent(){
        if(students.size() == 0){
            System.out.println("当前无学生信息,请添加后在查询");
            return;
        }
        showStudent();
        System.out.println("请输入要删除学生的ID:");
        int id = scanner.nextInt();
        for (Student stu:
                students) {
            if (stu.id == id){
                students.remove(stu);
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("id不存在");
    }

    static void updateStudent(){
        if(students.size() == 0){
            System.out.println("当前无学生信息,请添加后在查询");
            return;
        }
        showStudent();
        System.out.println("请输入要修改学生的id:");
        int id = scanner.nextInt();
        System.out.println("请输入把该学生的姓名修改为:");
        String name = scanner.next();
        System.out.println("请输入把该学生的年龄修改为:");
        int age = scanner.nextInt();
        System.out.println("请输入把该学生的家庭住址修改为:");
        String address = scanner.next();
        for (Student stu :
                students) {
            if(stu.id == id){
                stu.name = name;
                stu.age = age;
                stu.address = address;
                System.out.println("修改成功!");
                return;
            }
        }
        System.out.println("此id不存在!");
    }

    static void showStudent(){
        if(students.size() == 0){
            System.out.println("当前无学生信息,请添加后在查询");
            return;
        }
        for (Student stu:
             students) {
            System.out.println(stu);
        }
    }
}
